package com.zrf.gulimall.product.app;

import com.zrf.gulimall.common.database.utils.PageUtils;
import com.zrf.gulimall.commnon.core.utils.R;
import com.zrf.gulimall.commnon.core.valid.AddGroup;
import com.zrf.gulimall.commnon.core.valid.UpdateGroup;
import com.zrf.gulimall.commnon.core.valid.UpdateStatusGroup;
import com.zrf.gulimall.product.entity.BrandEntity;
import com.zrf.gulimall.product.entity.CategoryBrandRelationEntity;
import com.zrf.gulimall.product.service.BrandService;
import com.zrf.gulimall.product.service.CategoryBrandRelationService;
import com.zrf.gulimall.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 品牌
 *
 * @author zrf
 * @email 120662332@qq.com
 * @date 2021-01-23 16:07:19
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
  @Autowired
  private BrandService brandService;

  @Resource
  private CategoryBrandRelationService categoryBrandRelationService;

  /**
   * 列表
   */
  @RequestMapping("/list")
  //@RequiresPermissions("product:brand:list")
  public R list(@RequestParam Map<String, Object> params) {
    PageUtils page = brandService.queryPage(params);
    return R.ok().put("page", page);
  }

  /**
   * 信息
   */
  @RequestMapping("/info/{brandId}")
  //@RequiresPermissions("product:brand:info")
  public R info(@PathVariable("brandId") Long brandId) {
    BrandEntity brand = brandService.getById(brandId);
    return R.ok().put("brand", brand);
  }

  /**
   * 查询分类下的所有品牌
   */
  @GetMapping("/relation/list/byCat")
  //@RequiresPermissions("product:brand:info")
  public R relationsByCatId(@RequestParam("catId") Long catId) {
    List<BrandEntity> brandEntities = brandService.queryByCatalogId(catId);
    List<BrandVo> data = brandEntities.stream().map(item -> {
      BrandVo brandVo = new BrandVo();
      brandVo.setBrandId(item.getBrandId());
      brandVo.setBrandName(item.getName());
      return brandVo;
    }).collect(Collectors.toList());
    return R.ok().put("data", data);
  }

  /**
   * 关联列表
   */
  @GetMapping("/relation/list/byBrand")
  //@RequiresPermissions("product:categorybrandrelation:list")
  public R relationsByBrandId(@RequestParam("brandId") Long brandId) {
    List<CategoryBrandRelationEntity> data = categoryBrandRelationService.queryByBrandId(Collections.singletonList(brandId));
    return R.ok().put("data", data);
  }

  /**
   * 保存
   */
  @RequestMapping("/save")
  //@RequiresPermissions("product:brand:save")
  public R save(@Validated(AddGroup.class) @RequestBody BrandEntity brand) {
    brandService.save(brand);
    return R.ok();
  }

  /**
   * 关联分类
   */
  @RequestMapping("/relation/save")
  //@RequiresPermissions("product:brand:save")
  public R relationCategory(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
    categoryBrandRelationService.saveDetails(categoryBrandRelation);
    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  //@RequiresPermissions("product:brand:update")
  public R update(@Validated(UpdateGroup.class) @RequestBody BrandEntity brand) {
    brandService.updateDetails(brand);
    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/update/status")
  //@RequiresPermissions("product:brand:update")
  public R updateStatus(@Validated(UpdateStatusGroup.class) @RequestBody BrandEntity brand) {
    brandService.updateById(brand);
    return R.ok();
  }

  /**
   * 删除品牌
   */
  @RequestMapping("/delete")
  //@RequiresPermissions("product:brand:delete")
  public R deleteBrand(@RequestBody Long[] brandIds) {
    brandService.delete(Arrays.asList(brandIds));
    return R.ok();
  }

  /**
   * 移除关联
   */
  @RequestMapping("/delete/relation")
  //@RequiresPermissions("product:brand:delete")
  public R removeRelation(@RequestBody Long[] relationIds) {
    categoryBrandRelationService.removeByIds(Arrays.asList(relationIds));
    return R.ok();
  }

}
